<?php
/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Game
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Game_Plugin_Menus
{

	public function canFavoriteGames()
	{
    // Must be logged in
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }

    return true;
	}
	
  public function canCreateGames()
  {
    // Must be logged in
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }

    // Must be able to create games
    if( !Engine_Api::_()->authorization()->isAllowed('game', $viewer, 'create') ) {
      return false;
    }

    return true;
  }

  public function canViewGames()
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    
    // Must be able to view games
    if( !Engine_Api::_()->authorization()->isAllowed('game', $viewer, 'view') ) {
      return false;
    }

    return true;
  }  

  
  public function canManageGames()
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }    

    return true;
  }
  

  public function onMenuInitialize_GameGutterCreate($row)
  {
    // Must be logged in
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !$viewer || !$viewer->getIdentity() ) {
      return false;
    }

    // Must be able to create games
    if( !Engine_Api::_()->authorization()->isAllowed('game', $viewer, 'create') ) {
      return false;
    }

    //return false;
    
    return true;
  }

  public function onMenuInitialize_GameGutterEdit($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $game = Engine_Api::_()->core()->getSubject('game');

    if( !($game instanceof Game_Model_Game) ) {
      return false;
    }    
    
    if( !$game->authorization()->isAllowed($viewer, 'edit') ) {
      return false;
    }

    // Modify params
    $params = $row->params;
    $params['params']['game_id'] = $game->getIdentity();
    $params['params']['ref'] = 'profile';
    return $params;
  }

  public function onMenuInitialize_GameGutterDelete($row)
  {
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return false;
    }

    $viewer = Engine_Api::_()->user()->getViewer();
    $game = Engine_Api::_()->core()->getSubject('game');

    if( !($game instanceof Game_Model_Game) ) {
      return false;
    }
    
    if( !$game->authorization()->isAllowed($viewer, 'delete') ) {
      return false;
    }

    // Modify params
    $params = $row->params;
    $params['params']['game_id'] = $game->getIdentity();
    return $params;
  }
    
  
}